//
//  UIColor+Extension.swift
//  Swift_Weibo
//
//  Created by Tim on 2019/8/13.
//  Copyright © 2019 Tim. All rights reserved.
//

import UIKit

extension UIColor {
    
    /// 创建 16 进制颜色
    ///
    /// - Parameter hex: 16 进制颜色值，格式为: 0xff0000 = 红色
    convenience init(hex: Int32) {
        let r = (hex & 0xff0000) >> 16
        let g = (hex & 0x00ff00) >> 8
        let b = (hex & 0x0000ff)
        self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: 1)
    }
    
    /// 创建 RGBA 颜色，默认不透明度为 1
    ///
    /// - Parameters:
    ///   - r: 红色值 0 ~ 255
    ///   - g: 绿色值 0 ~ 255
    ///   - b: 蓝色值 0 ~ 255
    ///   - a: 透明值 0.1 ~ 1
    convenience init(r: UInt8, g: UInt8, b: UInt8, a: CGFloat = 1) {
        self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: a)
    }
    
    /// 获取随机颜色
    class var random : UIColor {
        get {
            return UIColor(r: UInt8(arc4random() % 256), g: UInt8(arc4random() % 256), b: UInt8(arc4random() % 256))
        }
    }
    
}
